package com.bx.implatform.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bx.implatform.entity.secondary.SecondaryAdmin;
import com.bx.implatform.entity.secondary.SecondaryAdminRole;
import com.bx.implatform.mapper.secondary.SecondaryAdminMapper;
import com.bx.implatform.service.SecondaryAdminRoleService;
import com.bx.implatform.service.SecondaryAdminService;
import com.bx.implatform.service.SecondaryUserService;
import com.bx.implatform.util.BeanUtils;
import com.bx.implatform.vo.SecondaryAdminVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Slf4j
@Service
@RequiredArgsConstructor
public class SecondaryAdminServiceImpl extends ServiceImpl<SecondaryAdminMapper, SecondaryAdmin> implements SecondaryAdminService {

    private final SecondaryAdminRoleService secondaryAdminRoleService;

    @Override
    public SecondaryAdmin findUserByAccount(String account) {
        LambdaQueryWrapper<SecondaryAdmin> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.like(SecondaryAdmin::getPhoneNumber, account).or().like(SecondaryAdmin::getEmail, account)
                .or().like(SecondaryAdmin::getWxId, account);
        SecondaryAdmin admin = this.getOne(queryWrapper);
        return admin;
    }

    @Override
    public SecondaryAdminVO findUserById(Integer id) {
        SecondaryAdmin secondaryAdmin = this.getById(id);
        SecondaryAdminVO secondaryAdminVO = BeanUtils.copyProperties(secondaryAdmin, SecondaryAdminVO.class);
        if (secondaryAdminVO != null && secondaryAdminVO.getId() != null) {
            SecondaryAdminRole secondaryAdminRole = secondaryAdminRoleService.findRoleByUserId(secondaryAdminVO.getId());
            if (secondaryAdminRole != null) {
                secondaryAdminVO.setRoleId(secondaryAdminRole.getRoleId());
            }
        }
        return secondaryAdminVO;
    }

}
